<template>
  <div class="row h-full businessItemForm" style="overflow-y: auto">
    <a-collapse v-model="activeKey">
      <a-collapse-panel key="1" header="业务事项映射基础信息">
        <a-row class="m-b-md">
          <a-col :span="3" class="text-align-right">
            <label class="m-r-lg">事项名称</label>
          </a-col>
          <a-col :span="8">
            <a-input
              :disabled="true"
              :value="currentItem.F_ITEM_NAME"/>
          </a-col>
          <a-col :span="3" class="text-align-right">
            <label class="m-r-lg">事项编码</label>
          </a-col>
          <a-col :span="8">
            <a-input
              :disabled="true"
              :value="currentItem.F_ITEM_CODE"/>

          </a-col>
        </a-row>
        <a-row class="m-b-md">
          <a-col :span="3" class="text-align-right">
            <label class="m-r-lg">业务名称</label>
          </a-col>
          <a-col :span="8">
            <a-input
              :disabled="true"
              :value="currentItem.F_BUSINESS_NAME"/>
          </a-col>
          <a-col :span="3" class="text-align-right">
            <label class="m-r-lg">业务ID</label>
          </a-col>
          <a-col :span="8">
            <a-input
              :disabled="true"
              :value="currentItem.F_BUSINESS_ID"/>
          </a-col>
        </a-row>
      </a-collapse-panel>
      <a-collapse-panel key="2" header="表单映射信息">
        <a-row class="m-b-md">
          <a-col :span="3" class="text-align-right">
            <label class="m-r-lg">表单类型</label>
          </a-col>
          <a-col :span="8">
            <!--            <a-select class="w-full"-->
            <!--                      v-model="currentItem.F_ITEM_TYPE"-->
            <!--                      :disabled=true>-->
            <!--              <a-select-option v-for="item in formTypes" :key="item.key">{{item.key}}</a-select-option>-->
            <!--            </a-select>-->
            <a-input :disabled="true" :value="currentItem.F_ITEM_TYPE"/>
          </a-col>
          <a-col :span="3" class="text-align-right">
            <label class="m-r-lg">事项字段</label>
          </a-col>
          <a-col :span="8">
            <a-select class="w-full" v-model="currentItem.F_CODE">
              <a-select-option v-for="item in investmentForms" :key="item.code">{{ item.code }}</a-select-option>
            </a-select>
          </a-col>
        </a-row>
        <a-row class="m-b-md">
          <a-col :span="3" class="text-align-right">
            <label class="m-r-lg">业务表</label>
          </a-col>
          <a-col :span="8">
            <a-input
              :disabled="true"
              :value="currentItem.F_BUSINESS_TABLE"/>

          </a-col>
          <a-col :span="3" class="text-align-right">
            <label class="m-r-lg">业务表字段</label>
          </a-col>
          <a-col :span="8">
            <a-input
              :disabled="true"
              :value="currentItem.F_BUSINESS_FIELD"/>

          </a-col>
        </a-row>
        <a-row class="m-b-md">
          <a-col :span="3" class="text-align-right">
            <label class="m-r-lg">转换类型</label>
          </a-col>
          <a-col :span="8">
            <a-input
              :disabled="true"
              :value="currentItem.F_ADAPTFIELDTYPE"/>

          </a-col>
          <span v-if="currentItem.F_ADAPTFIELDTYPE === 'dictionary'">
            <a-col :span="3" class="text-align-right">
              <label class="m-r-lg">数据字典值</label>
            </a-col>
            <a-col :span="8">
              <a-select class="w-full" v-model="currentItem.F_DICTIONARYID">
                <a-select-option
                  v-for="item in dictionaryOptions"
                  :key="item.id"
                  :value="item.id">{{ item.name }}</a-select-option>
              </a-select>
            </a-col>
          </span>
        </a-row>
      </a-collapse-panel>
    </a-collapse>

  </div>
</template>

<script>
import dapProjectService from '@/api/dapProjectService'
import dicService from '@/api/dicService'

export default {
  data () {
    return {
      activeKey: ['1', '2'],
      formTypes: [
        {
          title: '系统表单',
          key: 'system'
        },
        {
          title: '业务表单',
          key: 'business'
        }
      ],
      currentItem: this.currentItemProps,
      investmentForms: [],
      dictionaryOptions: []
    }
  },
  methods: {
    async getInvestmentForm (formType) {
      console.log(formType)
      let result
      if (this.dataMode) {
        if (formType === 'system') {
          result = await dapProjectService.getInvestmentItemFormListByTypeSource({
            type: 'system',
            source: 'investment_three_new'
          })
        } else if (formType === 'business') {
          result = await dapProjectService.getInvestmentItemFormListByTypeSource({
            itemCode: this.currentItem.F_ITEM_CODE,
            source: 'investment_three_new'
          })
        }
      } else {
        if (formType === 'system') {
          result = await dapProjectService.getInvestmentItemFormListByTypeSource({
            type: 'system',
            source: 'investment_three_old'
          })
        } else if (formType === 'business') {
          result = await dapProjectService.getInvestmentItemFormListByTypeSource({
            itemCode: this.currentItem.F_ITEM_CODE,
            source: 'investment_three_old'
          })
        }
      }

      console.log(result)
      this.investmentForms = result
    },
    editItemForm () {
      const value = []
      const data = {
        id: this.currentItem.ID,
        businessItemMapId: this.currentItem.F_BUSINESS_ITEM_MAP_ID,
        businessTable: this.currentItem.F_BUSINESS_TABLE,
        businessField: this.currentItem.F_BUSINESS_FIELD,
        itemFormId: this.currentItem.F_ITEM_FORM_ID,
        itemCode: this.currentItem.F_ITEM_CODE,
        code: this.currentItem.F_CODE,
        itemName: this.currentItem.F_ITEM_NAME,
        itemType: this.currentItem.F_ITEM_TYPE,
        name: this.currentItem.F_NAME,
        adaptFieldType: this.currentItem.F_ADAPTFIELDTYPE,
        parentCode: this.currentItem.F_PARENTCODE,
        subTable: this.currentItem.F_SUBTABLE,
        dictionaryID: this.currentItem.F_DICTIONARYID,
        source: this.currentItem.F_SOURCE
      }
      value.push(data)
      console.log(JSON.stringify(value))
      dapProjectService.saveBusinessItemFormList(value).then((d) => {
        console.log(d)
      })
    },
    async getDictionaryOptions () {
      await dicService.searchDictionary().then((response) => {
        this.dictionaryOptions = response.rows
        console.log(this.dictionaryOptions)
      })
    }
  },
  props: ['currentItemProps'],
  watch: {
    activeKey (key) {
      console.log(key)
    }
  },
  mounted () {
    this.getInvestmentForm(this.currentItemProps.F_ITEM_TYPE)
    this.getDictionaryOptions()
    this.Event.$on('_EDIT_ITEMFORM_DATA', this.editItemForm)
  },
  computed: {
    dataMode: {
      get () {
        return this.$store.getters.getDataMode
      },
      set () {}
    }
  }

}
</script>

<style scoped lang="less">

</style>
